package com.cb.admin.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cb.admin.domain.ConfigInfo;
import com.cb.admin.service.IConfigInfoService;
import com.cb.constants.Constants;
import com.cb.util.JSONUtil;


@Controller
@RequestMapping("/configinfo")
public class ConfigInfoController extends BaseController{

	private static Logger logger = Logger.getLogger(ConfigInfoController.class.getName());
	
	@Autowired
	private IConfigInfoService configInfoService;

	@RequestMapping("show")
	public String show(Model model,ConfigInfo configInfo){
		return "/config/configInfoList";
	}

	@RequestMapping(path = "/getList", method = RequestMethod.POST)
	@ResponseBody
	public String getUserList(ConfigInfo configInfo) {
		//当前页  
		configInfo.setStart((configInfo.getPage()-1)*configInfo.getRows());
		configInfo.setLength(configInfo.getRows());
		
		JSONObject jo = new JSONObject();
		
		List<ConfigInfo> objs = configInfoService.query(configInfo);
		
		int size = configInfoService.querySize(configInfo);
		jo.put("rows", objs);
		jo.put("total", size);
		return jo.toString();
	}
	
	@RequestMapping("createPage")
	public String createPage(Model model,ConfigInfo configInfo){
		model.addAttribute("configInfo", configInfo);
		return "/config/configInfoDetail";
	}
	
	@RequestMapping(path = "/create", method = RequestMethod.POST)
	@ResponseBody
	public String create(Model model,ConfigInfo configInfo,HttpServletRequest request,HttpSession session){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		if(configInfo.getParentId()==null || "".equals(configInfo.getParentId())){
			configInfo.setParentId("0");
		}
		
		try {
			configInfo.setCreateUser(returnSessionObj(session).getAccount());
			configInfo.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
			configInfo.setUpdateUser(returnSessionObj(session).getAccount());
			configInfoService.insert(configInfo);
		} catch (Exception e) {
			// TODO: handle exception
			JSONUtil.returnFailReuslt(null);
		}
		
		return jo.toString();
	}
	
	@RequestMapping("edit")
	public String edit(Model model,@RequestParam("id") int id){
		ConfigInfo obj = configInfoService.queryById(id);
		model.addAttribute("configInfo", obj);
		return "/config/configInfoEdit";
	}
	
	@RequestMapping(path = "/update", method = RequestMethod.POST)
	@ResponseBody
	public String update(Model model,ConfigInfo configInfo,HttpServletRequest request,HttpSession session){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			configInfo.setUpdateUser(returnSessionObj(session).getAccount());
			configInfoService.update(configInfo);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return jo.toString();
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public String deleteById(Model model,@RequestParam("Ids") String Ids) {
		
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			configInfoService.deleteById(Integer.parseInt(id));
		}
		
		return jo.toString();
	}
}
